// components/city/city.js

import { provinceQuery } from '../../api/user';

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    islayout: false,
    istoggHeight: false,
    version: 79,
    showProvince: true,
    provinceList: [],
    province: '',
    provinceName: '',
    cityList: [],
    city: '',
    cityName: '',
    areaList: [],
    district: '',
    districtName: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showCity(){
      this.setData({
        istoggHeight: true,
        islayout: true,
      })
      this.getprovince(0,'')
    },
    // 关闭弹框
    closeAdd(){
      this.setData({
        istoggHeight: false,
        islayout: false,
      })
    },
    // 获取省份数据
    getprovince(type,id) {
      var _this = this;
      provinceQuery(id)
        .then(res => {
          // console.log(res, "查询商户省");
          if (res.resp_code == "000000") {
            let list = res.result.map(items=>{
              items.code = items.id
              return items
            })
            if(type == 0){
              _this.setData({
                provinceList: list,
              })
            }else if(type == 1){
              _this.setData({
                cityList: list,
              })
            }else if(type == 2){
              _this.setData({
                areaList: list,
              })
            }
          }else if(res.resp_code=="333333"){
            _this.closeAdd()
          //  _this.$parent.merchantTrueFalseBy=true
          } 
        })
    },
    // 选择省份
    getProvinceId(e){
      let code = e.target.dataset.code||e.currentTarget.dataset.code;
      let name = e.target.dataset.name||e.currentTarget.dataset.name;
      let index = e.target.dataset.index||e.currentTarget.dataset.index;
      let list = this.data.provinceList.map((item,i)=>{
        item.selected = false
        if(i == index){
          item.selected = true
        }
        return item
      })
      this.setData({
        province: code,
        provinceName: name,
        showProvince: false,
        showCity: true,
        showDistrict: false,
        oneac: false, // 去掉第一个 nav 的高亮
        twoac: true, // 给第二个 nav 添加高亮
        threeac: false, // 去掉第三个 nav 的高亮
        twoshow: true, // 控制第二个 nav 展示
        threeshow: false, // 控制第三个 nav 隐藏
        city: '',
        cityName: '',
        oneliIndex: index,
        showDeter: false,
        provinceList: list,
      })
      this.getprovince(1,code)
    },
    // nav 选择省份
    provinceSelected(){
      this.setData({
        showProvince: true,
        showCity: false,
        showDistrict: false,
        oneac: true,
        twoac: false,
        threeac: false,
      })
    },
    // 选择城市
    getCityId(e){
      let code = e.target.dataset.code||e.currentTarget.dataset.code;
      let name = e.target.dataset.name||e.currentTarget.dataset.name;
      let index = e.target.dataset.index||e.currentTarget.dataset.index;
      let list = this.data.cityList.map((item,i)=>{
        item.selected = false
        if(i == index){
          item.selected = true
        }
        return item
      })
      this.setData({
        city: code,
        cityName: name,
        oneac: false, // 第一个 nav 去掉高亮
        showProvince: false,
        showCity: false,
        showDistrict: true, // 展示区域选择列表
        twoac: false,
        threeac: true,
        threeshow: true,
        districtName: '',
        cityList: list,
      })
      this.getprovince(2,code)
    },
    // nav 选择城市
    citySelected(){
      this.setData({
        showProvince: false,
        showCity: true,
        showDistrict: false,
        oneac: false,
        twoac: true,
        threeac: false,
      })
    },
    // 选择区域
    getDistrictId(e){
      let code = e.target.dataset.code||e.currentTarget.dataset.code;
      let name = e.target.dataset.name||e.currentTarget.dataset.name;
      let index = e.target.dataset.index||e.currentTarget.dataset.index;
      let list = this.data.areaList.map((item,i)=>{
        item.selected = false
        if(i == index){
          item.selected = true
        }
        return item
      })
      this.setData({
        district: code,
        districtName: name,
        areaList: list,
        oneac: false,
        twoac: false,
        showDeter: true,
      })
    },
    // nav 选择区域
    districtSelected(){
      this.setData({
        showProvince: false,
        showCity: false,
        showDistrict: true,
        oneac: false,
        twoac: false,
        threeac: true,
      })
    },
    // 确定选择省市区
    confirm(){
      let _this = this
      if(_this.data.province == '' || _this.data.city == '' || _this.data.district == ''){
        wx.showToast({
          title: '请先选择省市区',
          icon: 'none',
        })
        return
      }
      let city = _this.data.city + ',' + _this.data.cityName
      let extra = {
        provinceId:_this.data.province+','+_this.data.provinceName,  
        cityCode:city,
        merprovince:_this.data.provinceName,
        mercity:_this.data.cityName,
        merarea:_this.data.city,
        areaId: _this.data.district + ',' + _this.data.districtName,
        areaCode: _this.data.district,
      }
      _this.closeAdd()
      _this.triggerEvent('parent',JSON.stringify(extra))
    },
  }
})
